package com.jishaokang.controller;

import com.jishaokang.base.Result;
import com.jishaokang.model.dto.User;
import com.jishaokang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Map;

/**
 * Created by NANA_Final on 2019/6/21.
 */
@RestController
@RequestMapping(value = "/api")
public class UserController {

    @Autowired
    private UserService userService;

    //------------------------------------------
    @RequestMapping(value = "/all/user/login",method = RequestMethod.POST)
    public Result userLogin(@RequestBody User user) throws NoSuchAlgorithmException {
        return userService.login(user);
    }

    @RequestMapping(value = "/all/user/addUser",method = RequestMethod.POST)
    public Result addUser(@RequestBody User user) throws NoSuchAlgorithmException {
        return userService.addUser(user);
    }

    @RequestMapping(value = "/all/user/sendVerification",method = RequestMethod.POST)
    public Result sendVerification(@RequestBody User user) throws IOException {
        return userService.sendVerification(user);
    }

    @RequestMapping(value = "/all/user/loginByVerification",method = RequestMethod.POST)
    public Result loginByVerification(@RequestBody Map<String, String> map) throws NoSuchAlgorithmException {
        String phone = map.get("phone");
        String verification = map.get("verification");
        return userService.loginByVerification(phone,verification);
    }

    @RequestMapping(value = "/user/user/updatePhone",method = RequestMethod.POST)
    public Result updatePhone(@RequestBody Map<String, String> map){
        String token = map.get("token");
        String phone = map.get("phone");
        String verification = map.get("verification");
        return userService.updatePhone(token,phone,verification);
    }

    @RequestMapping(value = "/user/user/logout",method = RequestMethod.POST)
    public Result userLogout(@RequestBody User user) {
        return userService.logout(user);
    }


    @RequestMapping(value = "/user/user/getUserCurrent",method = RequestMethod.POST)
    public Result getUserCurrent(@RequestBody User user) {
        return userService.getUserCurrent(user);
    }

    @RequestMapping(value = "/all/user/selectUserByUsername",method = RequestMethod.POST)
    public Result selectUserByUsername(@RequestBody User user) {
        return userService.selectUserByUsername(user);
    }

    @RequestMapping(value = "/all/user/selectUserByUserId",method = RequestMethod.POST)
    public Result selectUserByUserId(@RequestBody User user) {
        return userService.selectUserByUserId(user);
    }

    @RequestMapping(value = "/user/user/updateUserPassword",method = RequestMethod.POST)
    public Result updateUserPassword(@RequestBody Map<String, String> map) throws NoSuchAlgorithmException {
        String token = map.get("token");
        String oldPassword = map.get("oldPassword");
        String password = map.get("password");
        return userService.updateUserPassword(token,oldPassword,password);
    }

    @RequestMapping(value = "/user/user/updateUser",method = RequestMethod.POST)
    public Result updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @RequestMapping(value = "/user/user/uploadUserImage",method = RequestMethod.POST)
    public Result updateUserImage(@RequestParam String token, @RequestParam(value = "userImageFile", required = false) MultipartFile imageFile, HttpServletRequest request) throws IOException {
        return userService.updateUserImage(token,imageFile,request);
    }

    //------------------------------------------
    @RequestMapping(value = "/examiner/user/login",method = RequestMethod.POST)
    public Result examinerLogin(@RequestBody User user) throws NoSuchAlgorithmException {
        return userService.login(user);
    }

    @RequestMapping(value = "/examiner/user/selectUserByUsername",method = RequestMethod.POST)
    public Result examinerSelectUserByUsername(@RequestBody User user) {
        return userService.selectUserByUsername(user);
    }


    @RequestMapping(value = "/examiner/user/updateUserPower",method = RequestMethod.POST)
    public Result updateUserPower(@RequestBody User user) throws ParseException {
        return userService.updateUserPower(user);
    }

    @RequestMapping(value = "/examiner/user/selectUserByForbid",method = RequestMethod.POST)
    public Result selectUserByForbid(@RequestBody User user) {
        return userService.selectUserByForbid(user);
    }

    @RequestMapping(value = "/examiner/user/logout",method = RequestMethod.POST)
    public Result examinerLogout(@RequestBody User user) {
        return userService.logout(user);
    }

    //-------------------------------------------
    @RequestMapping(value = "/admin/user/login",method = RequestMethod.POST)
    public Result adminLogin(@RequestBody User user) throws NoSuchAlgorithmException {
        return userService.login(user);
    }

    @RequestMapping(value = "/admin/user/selectUser",method = RequestMethod.POST)
    public Result selectUser(@RequestBody User user) {
        return userService.selectUser(user);
    }

    @RequestMapping(value = "/admin/user/selectUserByUsernameVague",method = RequestMethod.POST)
    public Result selectUserByUsernameVague(@RequestBody User user) {
        return userService.selectUserByUsernameVague(user);
    }

    @RequestMapping(value = "/admin/user/selectExaminer",method = RequestMethod.POST)
    public Result selectExaminer(@RequestBody User user) {
        return userService.selectExaminer(user);
    }

    @RequestMapping(value = "/admin/user/addExaminer",method = RequestMethod.POST)
    public Result addExaminer(@RequestBody User user) {
        return userService.addExaminer(user);
    }

    @RequestMapping(value = "/admin/user/deleteExaminer",method = RequestMethod.POST)
    public Result deleteExaminer(@RequestBody User user) {
        return userService.deleteExaminer(user);
    }

    @RequestMapping(value = "/admin/user/logout",method = RequestMethod.POST)
    public Result adminLogout(@RequestBody User user) {
        return userService.logout(user);
    }

}